package {
   import flash.display.DisplayObject;
   import flash.display.MovieClip;
   import flash.events.IOErrorEvent;
   import flash.events.Event;
   import flash.events.ProgressEvent;
   import flash.utils.getDefinitionByName;

   // Must be dynamic!
   public dynamic class Preloader extends MovieClip {
       // Keep track to see if an ad loaded or not
       private var did_load:Boolean;

       // Change this class name to your main class
       public static var MAIN_CLASS:String = "Main";

       [Embed(source="splash.png")]
       private var Splash:Class;
       private var splash:flash.display.Bitmap;

       private var start_t : int;

       public function Preloader() {
           super();
           splash = new Splash();
           splash.x = 0.5*(550-splash.width);
           splash.y = 30;
           addChild(splash);

           addEventListener(Event.ENTER_FRAME, showProgress);

           start_t = flash.utils.getTimer();
       }

       public function showProgress(e:Event):void {
        var percent:Number = 1.0 * root.loaderInfo.bytesLoaded /
 root.loaderInfo.bytesTotal;
        var dt  = flash.utils.getTimer()-start_t;
        percent = Math.min(percent, dt/1000.0/30.0); // <--- 3 sec magic constant here

        var w:Number = 200;
        var x = 0.5*(550 - w);
        var y = 520;

        this.graphics.clear()
        this.graphics.lineStyle(1, 0x880000, 1)
        this.graphics.drawRect(x,y,w,10)
        this.graphics.beginFill(0xff0000)
        this.graphics.drawRect(x,y,w*percent,10)

        // 3 sec magic constant here too

        if ((dt >= 30000) && root.loaderInfo.bytesLoaded ==
 root.loaderInfo.bytesTotal) {
           initApplication();
           removeEventListener(Event.ENTER_FRAME, showProgress);
        }
       }

       public function initApplication():void {
        var mainClass:Class = Class(getDefinitionByName(MAIN_CLASS));
        var app:Object = new mainClass();
        addChild(app as DisplayObject);
        if (app.init) {
           app.init(did_load);
        }
       }

   }

